package net.huashitong.ssydt.provider.money.service;

import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.money.entity.SsMoneyGold;
import net.huashitong.ssydt.money.entity.SsMoneyVip;
import net.huashitong.ssydt.money.entity.SsMoneyVipRecord;
import net.huashitong.ssydt.money.entity.vo.SsMoneyVipQueryInVO;
import net.huashitong.ssydt.money.entity.vo.SsMoneyVipRecordInVO;
import net.huashitong.ssydt.provider.money.mapper.SsMoneyVipMapper;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.08.18
 */
@Service
@Transactional
public class SsMoneyVipServiceImpl implements SsMoneyVipService {
	/**
	 * 一天的毫秒数
	 */
	private static final long DAY_MILLISECOND = 86400000L;

	private SsMoneyVipMapper mapper;
	private SsMoneyGoldService ssMoneyGoldService;
	private SsMoneyVipRecordService ssMoneyVipRecordService;
	private SsMoneyOrderService ssMoneyOrderService;

	@Autowired
	public SsMoneyVipServiceImpl(SsMoneyVipMapper mapper,
								 SsMoneyGoldService ssMoneyGoldService,
								 SsMoneyVipRecordService ssMoneyVipRecordService,
								 SsMoneyOrderService ssMoneyOrderService) {
		this.mapper = mapper;
		this.ssMoneyGoldService = ssMoneyGoldService;
		this.ssMoneyVipRecordService = ssMoneyVipRecordService;
		this.ssMoneyOrderService = ssMoneyOrderService;
	}

	@Override
	public SsMoneyVip getByUserId(String userId) {
		return mapper.getByUserId(userId);
	}

	@Override
	public void save(SsMoneyVip ssMoneyVip) {
		mapper.insert(ssMoneyVip);
	}

	@Override
	public void updateSelective(SsMoneyVip ssMoneyVip) {
		mapper.updateByPrimaryKeySelective(ssMoneyVip);
	}

	@Override
	public void renewalEffectiveDay(SsMoneyVipRecordInVO ssMoneyVipRecordInVO) {
		SsMoneyGold ssMoneyGold = ssMoneyGoldService.getByUserId(ssMoneyVipRecordInVO.getUserId());
		if (ssMoneyGold == null) {
			throw new ServiceException("用户的账户不存在，不能开通VIP套餐");
		}

		// 变更vip信息
		Date nowDate = new Date(System.currentTimeMillis());
		Date recordGmtStart = nowDate;
		SsMoneyVip ssMoneyVip = getByUserId(ssMoneyVipRecordInVO.getUserId());
		long mills = DAY_MILLISECOND * ssMoneyVipRecordInVO.getEffectiveDay(); // 有效天数转成毫秒数
		if (ssMoneyVip == null) {
			if (mills < 0) mills = 0; //新增vip时，有效天数不应该会是负数
			ssMoneyVip = new SsMoneyVip();
			ssMoneyVip.setUserId(ssMoneyVipRecordInVO.getUserId());
			ssMoneyVip.setGmtStart(recordGmtStart);
			ssMoneyVip.setGmtEnd(new Date(recordGmtStart.getTime() + mills));
			save(ssMoneyVip);
		} else {
			String nowDateStr = DateUtils.formatDate(recordGmtStart, "yyyyMMdd");
			String gmtEndStr = DateUtils.formatDate(ssMoneyVip.getGmtEnd(), "yyyyMMdd");
			if (nowDateStr.compareTo(gmtEndStr) <= 0) {
				recordGmtStart = ssMoneyVip.getGmtEnd();
			} else {
				ssMoneyVip.setGmtStart(recordGmtStart);
			}
			// 新的结束时间不早于当前时间（当进行扣除天数计算时）
			Date gmtDate = new Date(recordGmtStart.getTime() + mills);
			if (gmtDate.before(nowDate)) {
				gmtDate = nowDate;
			}
			ssMoneyVip.setGmtEnd(gmtDate);
			updateSelective(ssMoneyVip);
		}

		// 保存vip开通记录
		SsMoneyVipRecord ssMoneyVipRecord = new SsMoneyVipRecord();
		ssMoneyVipRecord.setUserId(ssMoneyVipRecordInVO.getUserId());
		ssMoneyVipRecord.setVipBagId(null);
		ssMoneyVipRecord.setGmtStart(recordGmtStart);
		ssMoneyVipRecord.setGmtEnd(ssMoneyVip.getGmtEnd());
		ssMoneyVipRecord.setGmtBuy(nowDate);
		ssMoneyVipRecord.setOrderId("");
		ssMoneyVipRecord.setRemarks(ssMoneyVipRecordInVO.getRemarks());
		ssMoneyVipRecord.setEffectiveDay(ssMoneyVipRecordInVO.getEffectiveDay());
		ssMoneyVipRecordService.save(ssMoneyVipRecord);
	}

	@Override
	public PaginationResult<SsMoneyVip> paginationQuery(SsMoneyVipQueryInVO ssMoneyVipQueryInVO) {
		PaginationResult result = new PaginationResult();
		result.setPageNumber(ssMoneyVipQueryInVO.getPageNumber());
		result.setPageSize(ssMoneyVipQueryInVO.getPageSize());

		// 查询记录总数
		int total = mapper.queryCount(ssMoneyVipQueryInVO);
		result.setPageRecords(total);

		if (total == 0) {
			result.setList(new ArrayList());
		} else {
			// 查询当前页记录
			List<SsMoneyVip> list = mapper.query(ssMoneyVipQueryInVO);
			result.setList(list);
		}
		return result;
	}
}
